/*
	The MIT License

	Copyright (c) 2008 IFMO/GameDev Studio

	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in
	all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	THE SOFTWARE.
*/
// ui_text.cpp - 

#include "ui_local.h"


/*-----------------------------------------------------------------------------
	Mono-type Text Rendering :
-----------------------------------------------------------------------------*/

//
//	EUserInterface::DrawString
//
void EUserInterface::DrawStringMT(float x, float y, float size, float tracking, const char *font_raster, 
								  const char *string, const EVec4 &color /* = EVec4 */)
{
	uint len = (uint)strlen(string);
	vector<quad_s> quads;
	
	EVec2 cursor(x - (size-tracking)/2, y - size);
	
	for (uint i=0; i<len; i++) 
	{
		quad_s q;
		q.color	=	color;
		q.p0 = cursor;
		q.p1 = cursor + EVec2(size, size);
		cursor += EVec2(tracking, 0);

		byte ch = string[i];
		float offset_x = (ch % 16) / 16.0f;
		float offset_y = (ch / 16) / 16.0f;
		const float one_16 = 1.0f / 16.0f;
		
		q.uv0 = EVec2(offset_x, offset_y);
		q.uv1 = EVec2(offset_x, offset_y) + EVec2(one_16, one_16);
		
		quads.push_back(q);
	}

	if (quads.size()!=0) {
		rs->DrawQuadList((uint)quads.size(), &quads[0], font_raster);
	}
}


static const EVec4 UI_TEXT_COLORS[8] = {
		EVec4(0,0,0,1),
		EVec4(0,0,1,1),
		EVec4(0,1,0,1),
		EVec4(0,1,1,1),
		EVec4(1,0,0,1),
		EVec4(1,0,1,1),
		EVec4(1,1,0,1),
		EVec4(1,1,1,1),
	};

//
//	EUserInterface::DrawStringMTColored
//
void EUserInterface::DrawStringMTClr(float x, float y, float size, float tracking, const char *font_raster, const char *string)
{
	uint len = (uint)strlen(string);
	vector<quad_s> quads;
	
	EVec2 cursor(x - (size-tracking)/2, y - size);
	EVec4 color = UI_TEXT_COLORS[7];
	
	for (uint i=0; i<len; i++) 
	{
		byte ch = string[i];
		
		if (ch=='^') {
			i++;
			ch = string[i];
			
			if (ch=='0') color = UI_TEXT_COLORS[0];
			if (ch=='1') color = UI_TEXT_COLORS[1];
			if (ch=='2') color = UI_TEXT_COLORS[2];
			if (ch=='3') color = UI_TEXT_COLORS[3];
			if (ch=='4') color = UI_TEXT_COLORS[4];
			if (ch=='5') color = UI_TEXT_COLORS[5];
			if (ch=='6') color = UI_TEXT_COLORS[6];
			if (ch=='7') color = UI_TEXT_COLORS[7];
			if (ch!='^') continue; // sequences like "^^ABC" should be transformed to "^ABC"
		}
	
		quad_s q;
		q.color	= color;
		q.p0 = cursor;
		q.p1 = cursor + EVec2(size, size);
		cursor += EVec2(tracking, 0);

		float offset_x = (ch % 16) / 16.0f;
		float offset_y = (ch / 16) / 16.0f;
		const float one_16 = 1.0f / 16.0f;
		
		q.uv0 = EVec2(offset_x, offset_y);
		q.uv1 = EVec2(offset_x, offset_y) + EVec2(one_16, one_16);
		
		quads.push_back(q);
	}
	
	rs->DrawQuadList((uint)quads.size(), &quads[0], font_raster);
}


/*-----------------------------------------------------------------------------
	Advanced Font Text Rendering
-----------------------------------------------------------------------------*/

//
//	EUserInterface::RegisterFont
//
IFont *EUserInterface::RegisterFont(const char *path)
{
	for (uint i=0; i<font_list.size(); i++) {
		if (strcmp(font_list[i]->Name(), path)==0) {
			return font_list[i];
		}
	}

	EFont *fnt = new EFont(this, path);
	font_list.push_back(fnt);
	return fnt;
}


//
//	EUserInterface::FreeFonts
//
void EUserInterface::FreeFonts(void)
{
	for (uint i=0; font_list.size(); i++) {
		delete font_list[i];
	}
	font_list.clear();
}


//
//	EUserInterface::ListFonts_f
//
void EUserInterface::ListFonts_f(EUserInterface *self, int argc, const char **argv)
{
	IConsole *con = self->con;
	LOGF("\r\n");
	for (uint i=0; i<self->font_list.size(); i++)	{
		self->font_list[i]->ShowInfo();
	}
	LOGF("%d fonts\r\n", self->font_list.size());
}


//
//	EUserInterface::DrawStringAdv
//
void EUserInterface::DrawStringFont(float x, float y, const IFont *font, const char *string, const EVec4 &color)
{
	if (!font) {
		return;
	}
	
	float scale = 1.0f;

	uint len = (uint)strlen(string);
	vector<quad_s> quads;
	quads.reserve(len);
	
	EVec2 cursor(x, y - font->GetLineHeight() * scale);
	
	for (uint i=0; i<len; i++) 
	{
		quad_s q;

		uchar ch   = string[i];
		uchar next = string[i+1];

		q.p0	=	cursor + font->GetOffset(ch) * scale;
		q.p1	=	q.p0 + font->GetGlyphSize(ch) * scale;
		q.uv0	=	font->GetUV0(ch);
		q.uv1	=	font->GetUV1(ch);
		q.color	=	color;
		
		cursor.x += (font->GetXAdvance(ch) * scale);
		
		cursor.x += ((float)font->GetKerning(ch, next) * scale);
		
		quads.push_back(q);
	}
	
	rs->DrawQuadList((uint)quads.size(), &quads[0], font->GetFontImage());
}



//
//	EUserInterface::GetStringLengthAdv
//
float EUserInterface::StringFontLength(const IFont *font, const char *string)
{
	float length = 0;
	uint len = (uint)strlen(string);

	for (uint i=0; i<len; i++) 
	{
		uchar ch   = string[i];
		uchar next = string[i+1];

		length += font->GetXAdvance(ch);
		length += (float)font->GetKerning(ch, next);
	}
	
	return length;
}


//
//	EUserInterface::DrawTextInRect
//
void EUserInterface::DrawTextInRect(uiRect_s &r, const IFont *font, const char *text, uint align, const EVec4 &color)
{
	string buffer = "";
	
	uint  str_len	= (uint)strlen(text);
	float dots_len	= StringFontLength(font, "...");
	float lh	    = font->GetLineHeight();
	float bl		= font->GetBaseLine();

	if (r.w<dots_len) {
		return;
	}

	for (uint i=0; i<str_len; i++) {
	
		if (StringFontLength(font, buffer.c_str()) + dots_len > r.w) {
			buffer.resize(buffer.length()-1);
			buffer += "...";
			break;
		}
		
		buffer.push_back(text[i]);
	}

	float text_len	= StringFontLength(font, buffer.c_str());
	float tx = r.x;
	float ty = r.y;// + (r.h + lh) / 2.0f;
	
	if (align & UI_ALIGNH_CENTER ){
		tx = r.x + (r.w-text_len)/2.0f;
	} 
	if (align & UI_ALIGNH_LEFT ){
		tx = r.x;
	} 
	if (align & UI_ALIGNH_RIGHT ){
		tx = r.x + r.w - text_len;
	} 
	
	if (align & UI_ALIGNV_TOP ){
		ty = r.y + lh;
	} 
	if (align & UI_ALIGNV_BOTTOM ){
		ty = r.y + r.h;
	} 
	if (align & UI_ALIGNV_CENTER ){
		ty = r.y + (r.h + lh) / 2.0f;
	} 
	
	
	
	DrawStringFont(floor(tx),   floor(ty), font, buffer.c_str(), color);
}














